There are many packages that can be used to create g maps in R. In fact, ggplot (which is part of the tidyverse) has the ability to make great looking maps. In this class, we will be using the tmap package. Tmap is a great package for introductory mapping because the commands are relatively simple and it is easy to adjust the data classification method. This document will introduce you to the very basics of using tmap. Some other good resources are below:

Basic Mapping

tmap: get started!

tmap

Basic Mapping in Tmap

Polygons

library(tmap)
library(sf)
library(rnaturalearth)
data("World")

tm_shape(World) + tm_polygons()

In this case, we use the tm_polygons() command because the features in the World object are polygons. If you are not sure of the geometry type of an object you can use the st_geometry_type() command

st_geometry_type(World, by_geometry = FALSE)
## [1] MULTIPOLYGON
## 18 Levels: GEOMETRY POINT LINESTRING POLYGON MULTIPOINT ... TRIANGLE

Points

# Extract centroids
country_centroids <- st_centroid(World)

tm_shape(country_centroids) + tm_dots()

Lines

rivers <- ne_download(scale = 50, category = 'physical', type = 'rivers_lake_centerlines')
## Reading layer `ne_50m_rivers_lake_centerlines' from data source 
##   `/private/var/folders/yc/bq8v5jzj1q9gsbv_rwvqfsgm0000gn/T/Rtmpzu1pLw/ne_50m_rivers_lake_centerlines.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 478 features and 36 fields (with 1 geometry empty)
## Geometry type: MULTILINESTRING
## Dimension:     XY
## Bounding box:  xmin: -165.2439 ymin: -50.24014 xmax: 176.3258 ymax: 73.3349
## Geodetic CRS:  WGS 84
tm_shape(rivers) + tm_lines()

Styling Maps

Changing Colors and Sizes

tm_shape(country_centroids) + tm_dots(col = "red")

tm_shape(country_centroids) + tm_dots(size = .5, col = "red")

tm_shape(rivers) + tm_lines(col = "blue")

tm_shape(rivers) + tm_lines(lwd = 3, col = "blue")

Displaying Variables

This is great, but often we want to display more on a map than just the outlines. You can display variables in tmap too. Note that tmap usually recognizes the difference between different data types. Since “HPI” is a continous variable, tmap has chosen to represent it on a color ramp. Since “continent” is categorical, it has used random colors. Note that if a categorical variable is represented as numeric, tmap will not be able to recognize it is categorical.

tm_shape(World) +
    tm_polygons("HPI")

tm_shape(World) +
    tm_polygons("continent")

tm_shape(rivers) +
    tm_lines("featurecla")

tm_shape(country_centroids) + tm_dots("life_exp")

Adding Multiple Layers

tm_shape(World) +
    tm_polygons("continent") + tm_shape(rivers) + tm_lines()
## Warning: The shape rivers contains empty units.

Changing Classification Scheme

tm_shape(World) +
    tm_polygons("HPI", style = "quantile")

tm_shape(World) +
    tm_polygons("HPI", style = "jenks")

Making Interactive Maps

You can also embed interactive maps in RMarkdowns by using the tmap_mode() command

tmap_mode("view")
## tmap mode set to interactive viewing
tm_shape(World) +
    tm_polygons("HPI")